package com.shop.common.partner.jiaotai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shop.util.DecimalUtil;
import com.shop.util.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class JiaotaiRequest {

    @Value("${com.shop.partner.jiaotai.host}")
    private String jiaotaiHost;

    private static final String merCode = "10009";
    private static final String secret = "wEoVP1GgBp2k1W344colq1";

    private Map<String, String> headerParam() {
        Map<String, String> header = new HashMap<>();
        header.put("platform", "h5");
        return header;
    }


    public JiaotaiBaseRespVo jtTicketLogin(String jtTicket) {
        // url
        String url = jiaotaiHost + "/user/simpleInfo?merNo=" + merCode;

        // header
        Map<String, String> header = headerParam();
        header.put("jtticket", jtTicket);

        // request body
        JSONObject reqBody = new JSONObject();

        // request
        Map<String, Object> respMap = HttpUtil.postJson(url, header, reqBody.toJSONString());
        if(!HttpUtil.isSucc200(respMap)) {
            log.error("请求获取用户信息异常！ response -> " + JSON.toJSONString(respMap));
            return null;
        }

        String respBodyStr = respMap.get("body").toString();
        return JSON.parseObject(respBodyStr, JiaotaiBaseRespVo.class);
    }


    public JiaotaiBaseRespVo rechargeOrder(String orderNo, BigDecimal amount, Integer payType, String notifyUrl) {
        // url
        String url = jiaotaiHost + "/merchant/rechargeOrder?merNo=" + merCode;

        // header
        Map<String, String> header = headerParam();

        // request body
        JSONObject reqBody = new JSONObject();
        reqBody.put("orderNo", orderNo);
        reqBody.put("amount", DecimalUtil.str(amount));
        reqBody.put("payType", payType);
        reqBody.put("notifyUrl", notifyUrl);

        // request
        Map<String, Object> respMap = HttpUtil.postJson(url, header, reqBody.toJSONString());
        if(!HttpUtil.isSucc200(respMap)) {
            log.error("请求获取支付参数信息异常！ response -> " + JSON.toJSONString(respMap));
            return null;
        }

        String respBodyStr = respMap.get("body").toString();
        JiaotaiBaseRespVo respVo = JSON.parseObject(respBodyStr, JiaotaiBaseRespVo.class);
        if(!respVo.getIsSuccess()) {
            log.error("请求获取支付参数信息失败！ respBodyStr -> " + respBodyStr);
            return null;
        }

        return respVo;
    }


    public JiaotaiBaseRespVo checkRechargeOrder(String orderNo) {
        // url
        String url = jiaotaiHost + "/merchant/checkRechargeOrder?merNo=" + merCode;

        // header
        Map<String, String> header = headerParam();

        // request body
        JSONObject reqBody = new JSONObject();
        reqBody.put("orderNo", orderNo);

        // request
        Map<String, Object> respMap = HttpUtil.postJson(url, header, reqBody.toJSONString());
        if(!HttpUtil.isSucc200(respMap)) {
            log.error("请求获取支付状态异常！ response -> " + JSON.toJSONString(respMap));
            return null;
        }

        String respBodyStr = respMap.get("body").toString();
        JiaotaiBaseRespVo respVo = JSON.parseObject(respBodyStr, JiaotaiBaseRespVo.class);
        if(!respVo.getIsSuccess()) {
            log.error("请求获取支付状态异常！ respBodyStr -> " + respBodyStr);
            return null;
        }

        return respVo;
    }



}
